//
//  DCWebViewController.m
//  Demo
//
//  Created by Kyle on 2022/12/31.
//

#import "DCWebViewController.h"
#import "DWKWebView.h"

@interface DCWebViewController ()<WKNavigationDelegate,WKUIDelegate>
@property (nonatomic, strong) DCWebModel *webInfo;

@property (nonatomic, strong) DWKWebView *webView;

@end

@implementation DCWebViewController

- (id)initWithModel:(DCWebModel *)model {
    self = [super init];
    if (self) {
        _webInfo = model;
    } return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitleName:self.webInfo.title];
    NSURL *url = [NSURL URLWithString:self.webInfo.url];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:req];
}

- (void)configureUI {
    [super configureUI];
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    WKUserContentController *controller = [[WKUserContentController alloc] init];
    
    configuration.userContentController = controller;
    configuration.allowsInlineMediaPlayback = YES;
    if (IOS10_ORLater) {configuration.mediaTypesRequiringUserActionForPlayback = NO;}
    
    WKPreferences *preferences = [WKPreferences new];
    preferences.javaScriptCanOpenWindowsAutomatically = YES;
    configuration.preferences = preferences;
    
    _webView = [[DWKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
    _webView.backgroundColor = [self.view backgroundColor];
    _webView.allowsBackForwardNavigationGestures = YES;
    _webView.DSUIDelegate = self;
    _webView.navigationDelegate = self;
    _webView.scrollView.bounces = NO;
    [self.view addSubview:_webView];
    
    [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(self.navgitBar.mas_bottom);
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
